import axios from 'axios';
import { Message,Alert } from "element-ui";
axios.defaults.baseURL = "http://45.145.75.2:8102"
// 登录请求方法
const loginreq = (method, url, params) => {
	return axios({
		method: method,
		url: url,
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded',
		},
		data: params,
		traditional: true,
		transformRequest: [
			function(data) {
				let ret = ''
				for (let it in data) {
					ret +=
						encodeURIComponent(it) +
						'=' +
						encodeURIComponent(data[it]) +
						'&'
				}
				return ret
			}
		]
	}).then(res => res.data);
};
// 通用公用方法
const req = (method, url, params) => {
	return axios({
		method: method,
		url: url,
		headers: {
			// 'Content-Type': 'application/x-www-form-urlencoded',
			"token": localStorage.getItem('LZY-TOKEN')
		},
		data: params,
	}).then(res => res.data).catch(err=>{
		Message.error('登录信息过期，请重新登录')
		// setTimeout(()=>{
		// 	window.location.href='/#/login';
		// },2000)
	});
};
let that = this
const getData = (method, url, params) => {
	return axios({
		method: method,
		url: url,
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded',
			"token": localStorage.getItem('LZY-TOKEN')
		},
		params: params,
	}).then(res => res.data).catch((err)=>{
		Message.error('登录信息过期，请重新登录')
        Alert('还未登录，请先登录！', '登录提醒', {
            confirmButtonText: '确定',
            callback: action => {
                window.location.href='/#/login';
            }
        })
		// setTimeout(()=>{
		// 	window.location.href='/#/login';
		// },2000)

	});
};

export {
	loginreq,
	req,
	getData
}
